<?php
/**
 * Workflow Process Step Page
 * This screen is divide in 2 grids, one for current candidate at
 * the current step and another with excluded candidate
 * 
 */

use Fox\Grid\FieldCat\Field\Renderer\Anchor;
use Fox\Generator;
use Fox\Behaviors;
use Fox\Html\Button;
use Hive\Workflow\Step;
use Fox\Dao\SqlFragment;
use Hive\Ressource;
use Fox\Grid;

$xview = Xview::instance();
$mid = $xview->val('mid');
$status = $xview->val('step');
//@TODO add authority check

/* @var $mission Hive\Mission */
$mission = $xview->component("missions", "object");
// Customizing
$customizing = Hive\Mission\Customizing::instance();
//$customizing->trackSummaryPage(array('mod' => 'missions','act'=>'process','sac' => 'step'));
try {
	$step = Fox\Dao::load(Step::className(),$status);
} catch(Fox\Object\Exception\NotExist $e) {
	//@TODO set a message
	return;
}

$html = null;
$handler = Fox\Databases::databases()->handle(Fox\Databases::DB_APP);
$query = $handler->createQueryBuilder();
$query->select('DISTINCT t.ressource_id','t.status','t.last_name','t.first_name','t.email','t.kn_number_year',
				'pa.comments',
				'a.phone1','a.phone2','a.city','a.postal_code','a.country',
				'exp.compid','exp.jobtitle','exp.salary','exp.bonus','c.company_name')
				  ->from('ressources','t')
				  ->innerJoin('t','mission_partners','pa','t.ressource_id = pa.partnerid')
				  ->leftJoin('t','address','a','t.adrid = a.adrid')
				  ->leftJoin('t','ressource_experiences','exp','t.ressource_id = exp.ressource_id AND current = 1')
				  ->leftJoin('t','companies','c','exp.compid = c.compid');
$fragment = new SqlFragment('pa.missionid = ? AND pa.role = ? AND pa.status = ? AND t.loekz = ?',
							array($mid,Hive\Mission\Partner::ROLE_CANDIDATE,
								$status,0));	  
$result = $fragment->pushIn($query)->execute()->fetchAll(PDO::FETCH_OBJ);

$header = new Fox\Html\Header\Content('messages',$step->label().' ['.count($result).']');
$gCurrent = new Grid('gCurrent');
$gCurrent->fieldcat(array(
		'first_name',
		'last_name',
		'email',
		'phone1',
		'experience',
		'kn_number_year',
		'salary',
		array('workflow_reason','comments')
		));
if($step->isClosing()) {
	$gCurrent->fieldcat()->field('experience')->noOutput();
	$gCurrent->fieldcat()->field('kn_number_year')->noOutput();
	$gCurrent->fieldcat()->field('salary')->noOutput();
} else {
	$gCurrent->fieldcat()->field('comments')->noOutput();
}

$gCurrent->fieldcat()->switchToObjectMode();
$gCurrent->setCoche();
$gCurrent->fieldcat()->setGenerators(array(
	'salary' => Fox\Generator::useFunction(function ($data,$key){
					return $data->salary + $data->bonus;
	}),
	'experience' => Fox\Generator::useFunction(function ($data,$key){
					return "<b>{$data->jobtitle}</b> chez {$data->company_name}";
	}),
	'coche' => Fox\Generator::useValue('')
));
$gCurrent->fieldcat()->behaviors(Fox\Behaviors::key(Fox\Generator::useAttribute("ressource_id")));
$gCurrent->layout()->scrollVertical(true);
$gCurrent->initialize($result);
$gCurrent->footer()->add(new Button('process',null,
						Msg(Hive::BUNDLE,Hive::MOVE)))
						->color(Button::COLOR_WARNING)
							->behaviors(Behaviors::action(
								Generator::url('missions','process','move',array('mid'=>$mid,'currentstep'=>$status)),
							true,null,null,true,'POST','bd_'.$gCurrent->id()));
$gCurrent->setFieldAttr(array("last_name","first_name","experience","email","phone1"),"renderers",
					new Anchor(Fox\Generator::url("ressource","main",null,
									array("rid" => Fox\Generator::useAttribute("ressource_id"))),true));
$html .= $gCurrent->display();
$keys = "&step=$status&mid=$mid";
?>
<div id="stepContent">
	<div class="source {'uri':'?mod=missions&act=process&sac=step&<?php echo $keys?>'}"><?php echo $header->render();?></div>
	<div class="" style="margin-left:15px">
		<div class="mbs gsl"">
				<?php echo $html; ?>
		</div>
	</div>
</div>
